package com.example.nna.voz;


import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.nna.main.R;
import com.example.nna.util.HorizontialListView;

public class pageReply extends Activity{
	private HorizontialListView mListEmo;
	private EditText m_TextPost;
	private Button m_ButPost;
	private Button m_ButPostCancle;
	private Button m_ButPostEmo;
	private ImageButton m_ButBold;
	private ImageButton m_ButItalic;
	private ImageButton m_ButUnder;
	private ImageButton m_ButLink;
	private ImageButton m_ButImage;
	private ArrayList<containEmo> listEmo;
	private Activity mActivity;
	private Context mContext;
	private String mType;
    private AlertDialog.Builder alert;
    private View textEntryView;
    private LayoutInflater factory;
    private EditText edit1 ;
    private EditText edit2 ;
    private TextView text1 ;
    private TextView text2 ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pagereply);
		mActivity = this;
		mContext = this;
		mListEmo = (HorizontialListView)findViewById(R.id.Page3_emo);
		m_TextPost = (EditText)findViewById(R.id.Page3_postReply);
		m_TextPost.setFocusable(true);
		m_TextPost.setTextSize(m_TextPost.getTextSize()*Voz.mTextSize);
		m_ButPost = (Button)findViewById(R.id.Page3_butReply);
		m_ButPostEmo = (Button)findViewById(R.id.Page3_butReplyEmo);
		m_ButPostCancle = (Button)findViewById(R.id.Page3_butReplyCancle);
		m_ButBold = (ImageButton)findViewById(R.id.PageReplyBold);
		m_ButItalic = (ImageButton)findViewById(R.id.PageReplyItalic);
		m_ButUnder= (ImageButton)findViewById(R.id.PageReplyUnder);
		m_ButLink= (ImageButton)findViewById(R.id.PageReplyLink);
		m_ButImage= (ImageButton)findViewById(R.id.PageReplyImage);
		m_ButBold.setOnClickListener(mBoldClick);
		m_ButItalic.setOnClickListener(mItalicClick);
		m_ButUnder.setOnClickListener(mUnderClick);
		m_ButImage.setOnClickListener(mImageClick);
		m_ButLink.setOnClickListener(mLinkClick);
		
		mListEmo.setOnItemClickListener(mEmoListClick);
		m_ButPost.setOnClickListener(mPostReplyClick);
		m_ButPostCancle.setOnClickListener(mPostCancleClick);
		m_ButPostEmo.setOnClickListener(mPostEmoClick);
		listEmo = new ArrayList<containEmo>();
		listEmo = Voz.mAssests.Emo2();
		listViewEmo adapter2 = new listViewEmo(mActivity, mHandler,listEmo);
	    mListEmo.setAdapter(adapter2);
		mType = getIntent().getExtras().getString("type");
		m_TextPost.setText(getIntent().getExtras().getString("msg"));
		m_TextPost.setSelection(m_TextPost.getText().length());
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

	}
	private void setPostVisible(int type) {
		m_ButPost.setVisibility(type);
		m_ButPostCancle.setVisibility(type);
		m_ButPostEmo.setVisibility(type);
		m_TextPost.setVisibility(type);
		
	}
	public final Handler mHandler = new Handler(){
    	@Override
    	public void handleMessage(Message msg) {
    		 switch (msg.what) {
    		 case 20:
    			int start =m_TextPost.getSelectionStart();
 				m_TextPost.getText().insert(start,(String) msg.obj);
     			break;	
    		 }
    	};
	};
	OnItemClickListener mEmoListClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			mHandler.obtainMessage(20,listEmo.get(arg2).text()).sendToTarget();
			
		}
		
	};
	OnClickListener mPostReplyClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			setPostVisible(View.GONE);
			mListEmo.setVisibility(View.GONE);
			Intent i = new Intent();
			Bundle extras = new Bundle();
 			extras.putString("type",mType);
 			extras.putString("msg",m_TextPost.getText().toString());
 			i.putExtras(extras);
			setResult(RESULT_OK,i);
            finish();
		}
	};
	OnClickListener mPostCancleClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			setResult(RESULT_CANCELED);
            finish();
		}
	};
	OnClickListener mImageClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			alertDialog();
			alertInsert();
			alert.setTitle("Please enter the url of your link");
			alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
		    	public void onClick(DialogInterface dialog, int whichButton) {
		    		int start =m_TextPost.getSelectionStart();
		    		String temp = "[IMG]"+edit1.getText().toString()+"[/IMG]";
					m_TextPost.getText().insert(start,temp);
		    	}
		    });
			alert.setNegativeButton("CANCLE", new DialogInterface.OnClickListener() {
		    	public void onClick(DialogInterface dialog, int whichButton) {
		    	}
		    });
			alert.show();
		}
	};
	OnClickListener mLinkClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			alertDialog();
			alertInsert();
			alert.setTitle("Please enter the url of your link");
			alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
		    	public void onClick(DialogInterface dialog, int whichButton) {
		    		int start =m_TextPost.getSelectionStart();
		    		String temp = "[URL=\""+edit1.getText().toString()+"\"]"+
		    				edit1.getText().toString()+"[/URL]";
					m_TextPost.getText().insert(start,temp);
		    	}
		    });
			alert.setNegativeButton("CANCLE", new DialogInterface.OnClickListener() {
		    	public void onClick(DialogInterface dialog, int whichButton) {
		    	}
		    });
			alert.show();
		}
	};
	OnClickListener mBoldClick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int start =m_TextPost.getSelectionStart();
				m_TextPost.getText().insert(start, "[B][/B]");
			}
		};
	OnClickListener mItalicClick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int start =m_TextPost.getSelectionStart();
				m_TextPost.getText().insert(start, "[I][/I]");
	
			}
		};
	OnClickListener mUnderClick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int start =m_TextPost.getSelectionStart();
				m_TextPost.getText().insert(start, "[U][/U]");
	
			}
		};
	OnClickListener mPostEmoClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(mListEmo.getVisibility() != View.VISIBLE){ 
				mListEmo.setVisibility(View.VISIBLE);
			}else mListEmo.setVisibility(View.GONE);
		}
	};
	private void alertDialog(){
	    factory = LayoutInflater.from(mContext);
	    textEntryView = factory.inflate(R.layout.login, null);    
	    edit1 = (EditText)textEntryView.findViewById(R.id.alert_edit1);
	    edit2 = (EditText)textEntryView.findViewById(R.id.alert_edit2);
	    text1 = (TextView)textEntryView.findViewById(R.id.alert_txt1);
	    text2 = (TextView)textEntryView.findViewById(R.id.alert_txt2);
	    alert = new AlertDialog.Builder(mContext);
	    alert.setView(textEntryView);
    }
	 private void alertInsert(){
	    	edit2.setVisibility(View.GONE);
	    	text2.setVisibility(View.GONE);
	    	text1.setVisibility(View.GONE);
	    	edit1.setText("http://");
	    	edit1.setSelection(edit1.getText().length());
	    }
}
